package ai.mspbots.poc.db.service.impl;

import ai.mspbots.poc.db.entity.AbbreviationMap;
import ai.mspbots.poc.db.mapper.AbbreviationMapMapper;
import ai.mspbots.poc.db.service.AbbreviationMapService;
import ai.mspbots.poc.help.VectorHelper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author jeffery
* @description 针对表【sys_report】的数据库操作Service实现
* @createDate 2024-09-06 17:01:26
*/
@Service
public class AbbreviationMapServiceImpl extends ServiceImpl<AbbreviationMapMapper, AbbreviationMap>
    implements AbbreviationMapService {

    private static final Logger logger = LoggerFactory.getLogger(AbbreviationMapServiceImpl.class);
    @Override
    public String getMapped(String suggestValue) {
        String replacePunctuationWithWord = VectorHelper.replacePunctuationWithWord(suggestValue, " ");
        logger.info("replacePunctuationWithWord = {}", replacePunctuationWithWord);
        List<String> stringList = StrUtil.split(replacePunctuationWithWord, " ");
        return stringList.stream().map(str -> {
            // 创建 QueryWrapper
            QueryWrapper<AbbreviationMap> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("LOWER(original)", str.toLowerCase());

            // 执行查询
            AbbreviationMap result = this.getOne(queryWrapper);
            if (result != null) {
                return result.getMapped();
            } else {
                return str;
            }
        }).collect(Collectors.joining(" "));
    }
}




